#define KeyPin  11     //按键开关引脚定义,红色
#define FkeyPin A5     //频率切换按键，白色

#define MAXFQUNUMBER 2    //可改变频率数量
#define RANDOMMM 5       //点击随机时间幅度

float hong = 9.0;         //红色按键频率
float bai = 9.5;          //白色按键频率

const int tPin[6][2] = {{2,3},{4,5},{6,7},{8,9},{A2,A1},{A4,A3}};  //1号引脚定义,灰的在前，白的在后，白的接电阻

void cleanAllTouch(){
  for(int i = 0;i<6;i++){
    digitalWrite(tPin[i][1], LOW);
    digitalWrite(tPin[i][0], HIGH);
  }
}

void touchPin(int p){
  if(p >=0 && p < 6){
    digitalWrite(tPin[p][1], HIGH);
    digitalWrite(tPin[p][0], LOW);
  }
}

void unTouchPin(int p){
  if(p >=0 && p < 6){
    digitalWrite(tPin[p][0], HIGH);
    digitalWrite(tPin[p][1], LOW);
  }
}

const int blinkdelay = 50;

void ledBlink(){
  for(int i = 0;i<5;i++){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(blinkdelay);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(blinkdelay); 
  }
}

void setup() {
  //初始化串口
  Serial.begin(115200);

  //设置随机数种子
  randomSeed(analogRead(A0));
  pinMode(KeyPin, OUTPUT);
  delay(1);
  digitalWrite(KeyPin, HIGH);
  pinMode(FkeyPin, OUTPUT);
  delay(1);
  digitalWrite(FkeyPin, HIGH);
  pinMode(KeyPin, INPUT_PULLUP); //设置按键引脚为输入
  pinMode(FkeyPin, INPUT); //设置按键引脚为输入

  pinMode(LED_BUILTIN, OUTPUT);
  //设置点击头控制引脚为输出
  for(int i = 0;i<6;i++){
    pinMode(tPin[i][0], OUTPUT);
    pinMode(tPin[i][1], OUTPUT);
  }
  delay(1);
  
  //初始化点击头状态,清除所有当关点击状态
  cleanAllTouch();
  digitalWrite(LED_BUILTIN, LOW);
}





int minTime0 = int(500/hong) - RANDOMMM;  //最小随机间隔时间
int maxTime0 = int(500/hong) + RANDOMMM;  //最大随机间隔时间

int minTime1 = int(500/bai) - RANDOMMM;  //最小随机间隔时间
int maxTime1 = int(500/bai) + RANDOMMM;  //最大随机间隔时间

int ttime = 125;
int utime = 125;

int minTTime_h = 60;
int maxTTime_h = 100;

int minTTime_b = 80;
int maxTTime_b = 110;

void loop() {
  int k = digitalRead(KeyPin);
  if(!k){ //红色
    int p = random(0,6);               //取随机按键
    ttime = random(minTTime_h,maxTTime_h);   //取随机时间
    touchPin(p);
    delay(ttime);
    utime = random(minTime0,maxTime0);   //取随机时间
    unTouchPin(p);
    delay(utime);
  }else{
    int fk = digitalRead(FkeyPin);
    if(!fk){ //白色
        int p = random(0,6);               //取随机按键
        ttime = random(minTTime_b,maxTTime_b);   //取随机时间
        touchPin(p);
        delay(ttime);
        utime = random(minTime1,maxTime1);   //取随机时间
        unTouchPin(p);
        delay(utime);        
    }
  }
}